package com.example.demo.webmagic;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.ResultItemsCollectorPipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

public class AlibabaPageProcesser implements PageProcessor {
    private String keyWords[] = new String[]{"coupling","wheel alignment"};
    private Site site = Site.me().setDomain("www.alibaba.com");

    @Override
    public void process(Page page) {
        List<String> links = page.getHtml().links().regex("https://www.alibaba.com/products/wheel_alignment.html").all();
        page.addTargetRequests(links);
        page.putField("title", page.getHtml().xpath("//div[@class='list-no-v2-outter J-offer-wrapper']").all());
        page.putField("content", page.getHtml().$("div.content").toString());
        page.putField("tags",page.getHtml().xpath("//div[@class='BlogTags']/a/text()").all());
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider spider = Spider.create(new AlibabaPageProcesser());

        ResultItemsCollectorPipeline result = new ResultItemsCollectorPipeline();

        spider.addUrl("https://www.alibaba.com/products/wheel_alignment.html").addPipeline(result).run();
        List<ResultItems> collector = result.getCollected();

    }
}
